package com.scalar.chiptrack.adaptor.unisem;

import com.scalar.chiptrack.jobs.JobHandler;
import com.scalar.chiptrack.adaptor.AdaptorConstants;
import com.scalar.chiptrack.adaptor.AdaptorUtils;
import com.scalar.chiptrack.adaptor.InventoryLot;
import com.scalar.chiptrack.adaptor.AssemblyLot;
import com.scalar.chiptrack.adaptor.ShippedLot;
import com.scalar.chiptrack.adaptor.ikanos.utl.UTLWipStatus;
import com.scalar.chiptrack.adaptor.dao.FabSortAdaptorDAO;
import com.scalar.chiptrack.adaptor.dao.ShippedLotDAO;
import com.scalar.chiptrack.adaptor.dao.AdaptorDAO;
import com.scalar.chiptrack.adaptor.dao.AdaptorRunInfo;
import com.scalar.chiptrack.adaptor.dao.InventoryLotDAO;
import com.scalar.chiptrack.adaptor.dao.AssemblyAdaptorDAO;
import com.scalar.chiptrack.utils.Logger;
import com.scalar.chiptrack.utils.StringUtils;
import com.scalar.chiptrack.db.DBTransaction;
import com.scalar.ScalarException;

import java.util.HashMap;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Date;
import java.util.Calendar;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

/**
 * Created by IntelliJ IDEA.
 * User: munugoti
 * Date: 9 Oct, 2009
 * Time: 2:32:58 PM
 * To change this template use File | Settings | File Templates.
 */
public class UNISEMDataParser extends JobHandler implements AdaptorConstants
{
    private static Logger m_parserLogger     = null;
    private String m_dataOutputDir           = null;

    private HashMap<String, AssemblyLot> m_assyWipDataMap = new HashMap<String, AssemblyLot>();
    private HashMap<String, InventoryLot> m_dieBankDataMap= new HashMap<String, InventoryLot>();

    Calendar cal = Calendar.getInstance();
    int thisYear = cal.get( Calendar.YEAR );
    int thisMonth = cal.get( Calendar.MONTH ) + 1 ;

    public UNISEMDataParser() throws ScalarException
    {
       try
       {
           m_dataOutputDir = AdaptorUtils.getDataOutputDir(VENDOR_NAME_UNISEM, WIP_TYPE_ASSEMBLY);
           AdaptorUtils.makeAllAdaptorDirs(m_dataOutputDir);
           m_parserLogger = AdaptorUtils.getDataParserLogger(m_dataOutputDir, VENDOR_NAME_UNISEM);
           InventoryLotDAO.setLogger(m_parserLogger);
           AssemblyAdaptorDAO.setLogger( m_parserLogger );
           ShippedLotDAO.setLogger(m_parserLogger);
           AdaptorUtils.setLogger( m_parserLogger);
       }
       catch ( ScalarException se )
       {
           log( "ERROR!! ERROR!! ERROR!! Exception while loading UNISEM WIP Status output directory details from database, Exception: " + se );
           throw se;
       }
    }

    public static void main( String args[] ) throws Exception
    {
       new UNISEMDataParser().execute();
    }


    public void execute() throws Exception
    {

       AdaptorUtils.printHeader(VENDOR_NAME_UNISEM, CONSTANT_PARSER);

       File doneDestDir = new File( m_dataOutputDir + "/done");

       File wipDataoutDir = new File( m_dataOutputDir );

       File[] fileList = wipDataoutDir.listFiles();

       if ( ( fileList == null ) || ( fileList.length <= 3 ) )
       {
           log( "No file found in directory: " + wipDataoutDir );
           throw new ScalarException( "No file found in directory: " + wipDataoutDir + "----\n\n" );
       }

       ArrayList successFileList    = new ArrayList();
       boolean success              = true;

       //PARSING STARTS FROM HERE
       for ( int i = 0; i < fileList.length; i++ )
       {

           if ( fileList[i].isDirectory() )
           {
               continue;
           }

           if ( ( fileList[i].getName() != null ) && ( fileList[i].getName().trim().startsWith ( UNISEMAdaptor.FILE_NAME_WIP_STATUS ) ) )
           {
               try
               {
                    // Parse the file and get the lot list
                    FileReader reader = new FileReader( fileList[i].getAbsolutePath() );

                    UNISEMHtmlWipParser htmlParser = new UNISEMHtmlWipParser();
                    log( "\nFound " + fileList[i].getAbsolutePath() );
                    log( "\nParsing the file for Wip Report..." );
                    System.out.println( "\nFound " + fileList[i].getAbsolutePath() );
                    System.out.println( "\nParsing the file for Wip Report..." );
                    //Here the HTML parsing starts
                    ArrayList<UNISEMWipStatus> m_wipStatusList = htmlParser.parse( reader );

                    if ( m_wipStatusList != null && m_wipStatusList.size() > 0 )
                    {
                        getWipStatusInfoList( m_wipStatusList );
                    }

                    //Add files to success list to move to /done directory
                    successFileList.add( fileList[i] );

                    try
                    {
                        if ( reader != null)
                        {
                            reader.close();
                            reader = null;
                        }
                    }
                    catch(IOException ignore)
                   {

                   }
               }
               catch ( Exception e )
               {
                   success = false;
                   log( "Exception while parsing and storing file: " + fileList[i].getName() + "\nException: " + e );
                   e.printStackTrace();
                   throw e;
               }

           }
      }
      // to save the Die Bank lot tables
       try
       {
           if ( m_dieBankDataMap.isEmpty() == false )
           {
               log(" Updating Die Bank Lot Information");
               InventoryLotDAO.saveInventoryLot( null, m_dieBankDataMap.values().iterator(), INVENTORY_TYPE_DIE_BANK, VENDOR_NAME_UNISEM );
               success = true;
           }
           else
           {
               InventoryLotDAO.closeInventoryLot(null, INVENTORY_TYPE_DIE_BANK, VENDOR_NAME_UNISEM  );
           }
      }
      catch (ScalarException se)
      {
          success = false;
          se.printStackTrace();
          log("ERROR!! ERROR!! ERROR!! Could not update Die Bank Lot information into database: " + se);
          throw se;
      }

       // to save the Assembly lot tables
       try
       {
           if ( m_assyWipDataMap.isEmpty() == false )
           {
               log(" Updating Assembly Lot Information");
               AssemblyAdaptorDAO.saveAssemblyLot( null, m_assyWipDataMap.values().iterator(), WIP_TYPE_ASSEMBLY, VENDOR_NAME_UNISEM );
               success = true;
           }
           else
           {
               AssemblyAdaptorDAO.closeAssemblyLot(null, WIP_TYPE_ASSEMBLY, VENDOR_NAME_UNISEM );
           }
      }
      catch (ScalarException se)
      {
          success = false;
          se.printStackTrace();
          log("ERROR!! ERROR!! ERROR!! Could not update Assembly Lot information into database: " + se);
          throw se;
      }
       // To Close the Drop Out Lots
       AdaptorDAO.closeDropOutLots( VENDOR_NAME_UNISEM );

       // TO SEND THE NEW LOTS, NEW TRANSIT LOTS, NO PO LOTS, DISCREPANCY LOTS NOTIFICATIONS TO THE USER
       AdaptorDAO.sendAllNotifications( VENDOR_NAME_UNISEM, true, true, true );


       int adaptor_id = AdaptorRunInfo.getLastRunAdaptorId( UNISEMAdaptor.ADAPTOR_NAME, CONSTANT_ADAPTOR);

        // Update Adaptor_Run_Info table
        try
        {
            if ( success == true )
            {
                AdaptorRunInfo.saveAdaptorLastUpdatedStatus(CONSTANT_PARSER, UNISEMAdaptor.ADAPTOR_NAME, CONSTANT_SUCCESS, adaptor_id);
                System.out.println( "SUCCESSFULLY UPDATED IN THE DATABASE" );
            }
        }
        catch ( ScalarException se )
        {
            success = false; //As filES DATA SAVED , UPDATEING AS SUCCESS
            AdaptorRunInfo.saveAdaptorLastUpdatedStatus(CONSTANT_PARSER, UNISEMAdaptor.ADAPTOR_NAME, CONSTANT_SUCCESS, adaptor_id);
            log( "WARNING!! WARNING!! WARNING!! Could not update Adaptor run information, Exception: " + se );
            throw se;
        }

       // Step 7
       // Move the successful files to 'done' directory
       if ( success )
       {
           AdaptorUtils.moveFilesToDone(successFileList, doneDestDir );
       }
   }//end of execute method

   /**
   *
   * @param wipdataList
   */
   public void getWipStatusInfoList( ArrayList wipdataList ) throws Exception
   {
       UNISEMWipStatus wipStatus = null;
       InventoryLot inventoryLot   = null;
       AssemblyLot assemblyLot     = null;
       ShippedLot shippedLot       = null;
       String partNumber           = null;
       String lotNumber            = null;
       String fabLotNumber         = null;

       try
        {
            Iterator iterator = wipdataList.iterator();

            while (iterator.hasNext())
            {
                wipStatus = (UNISEMWipStatus) iterator.next();

                fabLotNumber = wipStatus.getLotNumber();
                partNumber = wipStatus.getDeviceName();
                lotNumber  = wipStatus.getLotNumber();
                
                if ( lotNumber != null && lotNumber.trim().length() > 0 && partNumber != null && partNumber.trim().length() > 0)
                {
                    if ( lotNumber.indexOf("@") != -1 )
                    {
                        lotNumber = lotNumber.split("@")[0];
                    }
                    String receiveDate  = null;
                    String dateIn    = null;
                    String assyExpdateOut = null;

                    if ( wipStatus.getRecvDate() != null && wipStatus.getRecvDate().trim().length() < 8 )
                    {
                        if ( wipStatus.getRecvDate().indexOf("-") == -1 )
                        {
                            String[] recDate = wipStatus.getRecvDate().split("/");

                            int month = Integer.parseInt( recDate[1] );

                            if ( month > thisMonth )
                            {
                                receiveDate = month + "/" + recDate[0]   + "/" + ( thisYear-1);
                            }
                            else
                            {
                                receiveDate = month + "/" + recDate[0]  + "/" + thisYear;
                            }
                        }
                        else
                        {
                            String[] recDate = wipStatus.getRecvDate().split("-");
                            int month = AdaptorUtils.getJavaCalendarMonth( recDate[1] );
                            month = month + 1;

                            if ( month > thisMonth )
                            {
                                receiveDate = month + "/" + recDate[0]   + "/" + ( thisYear-1);
                            }
                            else
                            {
                                receiveDate = month + "/" + recDate[0]  + "/" + thisYear;
                            }
                        }
                    }
                    if ( wipStatus.getIqcDate() != null && wipStatus.getIqcDate().trim().length() < 8 )
                    {
                        if ( wipStatus.getIqcDate().indexOf("-") == -1 )
                        {
                            String[] recDate = wipStatus.getIqcDate().split("/");

                            int month = Integer.parseInt( recDate[1] );

                            if ( month > thisMonth )
                            {
                                dateIn = month + "/" + recDate[0]   + "/" + ( thisYear-1);
                            }
                            else
                            {
                                dateIn = month + "/" + recDate[0]  + "/" + thisYear;
                            }
                        }
                        else
                        {
                            String[] recDate = wipStatus.getIqcDate().split("-");
                            int month = AdaptorUtils.getJavaCalendarMonth( recDate[1] );
                            month = month + 1;

                            if ( month > thisMonth )
                            {
                                dateIn = month + "/" + recDate[0]   + "/" + ( thisYear-1);
                            }
                            else
                            {
                                dateIn = month + "/" + recDate[0]  + "/" + thisYear;
                            }
                        }

                    }

                    if (wipStatus.getCommitDate() != null && wipStatus.getCommitDate().trim().length() < 8 )
                    {
                        if ( wipStatus.getCommitDate().indexOf("-") == -1 )
                        {
                            String[] expDate = wipStatus.getCommitDate().split("/");
                            int month = Integer.parseInt( expDate[1] );

                            if ( month > thisMonth )
                            {
                                assyExpdateOut = month + "/" + expDate[0]  + "/" + ( thisYear-1);
                            }
                            else
                            {
                                assyExpdateOut = month + "/" + expDate[0]  + "/" + thisYear;
                            }
                        }
                        else
                        {
                            String[] expDate = wipStatus.getCommitDate().split("-");
                            int month = AdaptorUtils.getJavaCalendarMonth( expDate[1] );
                            month = month + 1;

                            if ( month > thisMonth )
                            {
                                assyExpdateOut = month + "/" + expDate[0]   + "/" + ( thisYear-1);
                            }
                            else
                            {
                                assyExpdateOut = month + "/" + expDate[0]  + "/" + thisYear;
                            }
                        }
                   }
                    //DIE BANK LOTS
                    if ( wipStatus.getDieBankQty() != null )
                    {
                        log("In Die Bank " + lotNumber );

                        inventoryLot = (InventoryLot) m_dieBankDataMap.get( partNumber + lotNumber );
                        if ( inventoryLot == null )
                        {
                            inventoryLot = new InventoryLot(VENDOR_NAME_UNISEM, LOCATION_TYPE_DIE_BANK );
                            m_dieBankDataMap.put( partNumber + lotNumber, inventoryLot );
                        }
                        int quantity = 0;
                        if (inventoryLot.getQtyIn() != null)
                        {
                            try
                            {
                                quantity = Integer.parseInt(inventoryLot.getQtyIn());
                            }
                            catch (NumberFormatException ignore) {
                            }
                            try
                            {
                                quantity += Integer.parseInt(wipStatus.getDieBankQty() );
                            }
                            catch (NumberFormatException nfe) {
                            }
                        }
                        else
                        {
                            try
                            {
                                quantity = Integer.parseInt( wipStatus.getDieBankQty() );
                            }
                            catch (NumberFormatException ignore) {
                            }
                        }

                        if ( quantity > 0 )
                        {
                            inventoryLot.setLotNumber( lotNumber );
                            inventoryLot.setPartNumber( partNumber );
                            inventoryLot.setStatus( LOT_STATUS_RUNNING );
                            inventoryLot.setFabLotNumber( lotNumber );
                            inventoryLot.setCategory( LOT_CATEGORY_TYPE_PROD );
                            inventoryLot.setQtyIn( quantity +"" );
                            inventoryLot.setDateIn( receiveDate );
                        }
                    }

                    //ASSEMBLY
                    if( wipStatus.getRcQty() != null || wipStatus.getDsQty() != null || wipStatus.getDpQty() != null || wipStatus.getDieAttach() != null
                        || wipStatus.getWireBond() != null || wipStatus.getCoating() != null || wipStatus.getMolding() != null || wipStatus.getDejk() != null
                        || wipStatus.getSluring() != null  || wipStatus.getMarking() != null || wipStatus.getMfQty() != null   || wipStatus.getTfQty() != null
                        || wipStatus.getFvQty() != null || wipStatus.getFinalTest() != null || wipStatus.getFgQty() != null)
                    {
                        log( "In Assembly " + lotNumber ) ;

                        assemblyLot = (AssemblyLot) m_assyWipDataMap.get( partNumber + lotNumber + fabLotNumber );
                        if (assemblyLot == null)
                        {
                            assemblyLot = new AssemblyLot( VENDOR_NAME_UNISEM );
                            m_assyWipDataMap.put( partNumber + lotNumber + fabLotNumber, assemblyLot);
                        }

                        // FOR SETTING THE QTY IN
                        if( wipStatus.getRcQty() != null )
                        {
                            assemblyLot = (AssemblyLot) m_assyWipDataMap.get(partNumber + lotNumber + fabLotNumber);

                            if( assemblyLot.getQtyIn() != null )
                            {
                                try
                                {
                                    assemblyLot.setQtyIn( ( Integer.parseInt( assemblyLot.getQtyIn() ) + Integer.parseInt( wipStatus.getRcQty() ) ) + "" );
                                }
                                catch(Exception e)
                                {
                                    assemblyLot.setQtyIn( wipStatus.getRcQty() );
                                }
                            }
                            else
                            {
                                assemblyLot.setQtyIn( wipStatus.getRcQty() );
                            }
                            if ( assemblyLot.getStage() != null && !"Raw Stock".equalsIgnoreCase( assemblyLot.getStage() ) )
                            {
                                assemblyLot.setStage ( assemblyLot.getStage() +", Raw Stock") ;
                            }
                            else
                            {
                                assemblyLot.setStage("Raw Stock");
                            }
                        }
                        if( wipStatus.getDsQty() != null )
                        {
                            assemblyLot = (AssemblyLot) m_assyWipDataMap.get(partNumber + lotNumber + fabLotNumber);

                            if( assemblyLot.getQtyIn() != null )
                            {
                                try
                                {
                                    assemblyLot.setQtyIn( ( Integer.parseInt( assemblyLot.getQtyIn() ) + Integer.parseInt( wipStatus.getDsQty() ) ) + "" );
                                }
                                catch(Exception e)
                                {
                                    assemblyLot.setQtyIn( wipStatus.getDsQty() );
                                }
                            }
                            else
                            {
                                assemblyLot.setQtyIn( wipStatus.getDsQty() );
                            }
                            if ( assemblyLot.getStage() != null && !"Die Store".equalsIgnoreCase( assemblyLot.getStage() ) )
                            {
                                assemblyLot.setStage ( assemblyLot.getStage() +", Die Store") ;
                            }
                            else
                            {
                                assemblyLot.setStage("Die Store");
                            }
                        }
                        if( wipStatus.getDpQty() != null )
                        {
                            assemblyLot = (AssemblyLot) m_assyWipDataMap.get(partNumber + lotNumber + fabLotNumber);

                            if( assemblyLot.getQtyIn() != null )
                            {
                                try
                                {
                                    assemblyLot.setQtyIn( ( Integer.parseInt( assemblyLot.getQtyIn() ) + Integer.parseInt( wipStatus.getDpQty() ) ) + "" );
                                }
                                catch(Exception e)
                                {
                                    assemblyLot.setQtyIn( wipStatus.getDpQty() );
                                }
                            }
                            else
                            {
                                assemblyLot.setQtyIn( wipStatus.getDpQty() );
                            }
                            if ( assemblyLot.getStage() != null && !"Die Prepare".equalsIgnoreCase( assemblyLot.getStage() ) )
                            {
                                assemblyLot.setStage ( assemblyLot.getStage() +", Die Prepare") ;
                            }
                            else
                            {
                                assemblyLot.setStage("Die Prepare");
                            }
                        }
                        if( wipStatus.getDieAttach() != null  )
                        {
                            assemblyLot = (AssemblyLot) m_assyWipDataMap.get(partNumber + lotNumber + fabLotNumber);

                            if( assemblyLot.getQtyIn() != null )
                            {
                                try
                                {
                                    assemblyLot.setQtyIn( ( Integer.parseInt( assemblyLot.getQtyIn() ) + Integer.parseInt( wipStatus.getDieAttach() ) ) + "" );
                                }
                                catch(Exception e)
                                {
                                    assemblyLot.setQtyIn( wipStatus.getDieAttach() );
                                }
                            }
                            else
                            {
                                assemblyLot.setQtyIn( wipStatus.getDieAttach() );
                            }
                            if ( assemblyLot.getStage() != null && !"Die Attach".equalsIgnoreCase( assemblyLot.getStage() ) )
                            {
                                assemblyLot.setStage ( assemblyLot.getStage() +", Die Attach") ;
                            }
                            else
                            {
                                assemblyLot.setStage("Die Attach");
                            }
                        }
                        if( wipStatus.getWireBond() != null )
                        {
                            assemblyLot = (AssemblyLot) m_assyWipDataMap.get(partNumber + lotNumber + fabLotNumber);

                            if( assemblyLot.getQtyIn() != null )
                            {
                                try
                                {
                                    assemblyLot.setQtyIn( ( Integer.parseInt( assemblyLot.getQtyIn() ) + Integer.parseInt( wipStatus.getWireBond() ) ) + "" );
                                }
                                catch(Exception e)
                                {
                                    assemblyLot.setQtyIn( wipStatus.getWireBond() );
                                }
                            }
                            else
                            {
                                assemblyLot.setQtyIn( wipStatus.getWireBond() );
                            }
                            if ( assemblyLot.getStage() != null && !"Wire Bond".equalsIgnoreCase( assemblyLot.getStage() ) )
                            {
                                assemblyLot.setStage ( assemblyLot.getStage() +", Wire Bond") ;
                            }
                            else
                            {
                                assemblyLot.setStage("Wire Bond");
                            }
                        }
                        
                        if( wipStatus.getCoating() != null  )
                        {
                            assemblyLot = (AssemblyLot) m_assyWipDataMap.get(partNumber + lotNumber + fabLotNumber);

                            if( assemblyLot.getQtyIn() != null )
                            {
                                try
                                {
                                    assemblyLot.setQtyIn( ( Integer.parseInt( assemblyLot.getQtyIn() ) + Integer.parseInt( wipStatus.getCoating() ) ) + "" );
                                }
                                catch(Exception e)
                                {
                                    assemblyLot.setQtyIn( wipStatus.getCoating() );
                                }
                            }
                            else
                            {
                                assemblyLot.setQtyIn( wipStatus.getCoating() );
                            }
                            if ( assemblyLot.getStage() != null && !"Coating".equalsIgnoreCase( assemblyLot.getStage() ) )
                            {
                                assemblyLot.setStage ( assemblyLot.getStage() +", Coating") ;
                            }
                            else
                            {
                                assemblyLot.setStage("Coating");
                            }

                        }
                        if( wipStatus.getMolding() != null  )
                        {
                            assemblyLot = (AssemblyLot) m_assyWipDataMap.get(partNumber + lotNumber + fabLotNumber);

                            if( assemblyLot.getQtyIn() != null )
                            {
                                try
                                {
                                    assemblyLot.setQtyIn( ( Integer.parseInt( assemblyLot.getQtyIn() ) + Integer.parseInt( wipStatus.getMolding() ) ) + "" );
                                }
                                catch(Exception e)
                                {
                                    assemblyLot.setQtyIn( wipStatus.getMolding() );
                                }
                            }
                            else
                            {
                                assemblyLot.setQtyIn( wipStatus.getMolding() );
                            }
                            if ( assemblyLot.getStage() != null && !"Molding".equalsIgnoreCase( assemblyLot.getStage() ))
                            {
                                assemblyLot.setStage ( assemblyLot.getStage() +", Molding") ;
                            }
                            else
                            {
                                assemblyLot.setStage("Molding");
                            }

                        }
                        if( wipStatus.getDejk() != null  )
                        {
                            assemblyLot = (AssemblyLot) m_assyWipDataMap.get(partNumber + lotNumber + fabLotNumber);

                            if( assemblyLot.getQtyIn() != null )
                            {
                                try
                                {
                                    assemblyLot.setQtyIn( ( Integer.parseInt( assemblyLot.getQtyIn() ) + Integer.parseInt( wipStatus.getDejk() ) ) + "" );
                                }
                                catch(Exception e)
                                {
                                    assemblyLot.setQtyIn( wipStatus.getDejk() );
                                }
                            }
                            else
                            {
                                assemblyLot.setQtyIn( wipStatus.getDejk() );
                            }
                            if ( assemblyLot.getStage() != null && !"Dejunk".equalsIgnoreCase( assemblyLot.getStage() ) )
                            {
                                assemblyLot.setStage ( assemblyLot.getStage() +", Dejunk") ;
                            }
                            else
                            {
                                assemblyLot.setStage("Dejunk");
                            }

                        }
                        if( wipStatus.getSluring() != null )
                        {
                            assemblyLot = (AssemblyLot) m_assyWipDataMap.get(partNumber + lotNumber + fabLotNumber);

                            if( assemblyLot.getQtyIn() != null )
                            {
                                try
                                {
                                    assemblyLot.setQtyIn( ( Integer.parseInt( assemblyLot.getQtyIn() ) + Integer.parseInt( wipStatus.getSluring() ) ) + "" );
                                }
                                catch(Exception e)
                                {
                                    assemblyLot.setQtyIn( wipStatus.getSluring() );
                                }
                            }
                            else
                            {
                                assemblyLot.setQtyIn( wipStatus.getSluring() );
                            }
                            if ( assemblyLot.getStage() != null && !"Slurry".equalsIgnoreCase( assemblyLot.getStage() ) )
                            {
                                assemblyLot.setStage ( assemblyLot.getStage() +", Slurry") ;
                            }
                            else
                            {
                                assemblyLot.setStage("Slurry");
                            }

                        }
                        if( wipStatus.getMarking() != null )
                        {
                            assemblyLot = (AssemblyLot) m_assyWipDataMap.get(partNumber + lotNumber + fabLotNumber);

                            if( assemblyLot.getQtyIn() != null )
                            {
                                try
                                {
                                    assemblyLot.setQtyIn( ( Integer.parseInt( assemblyLot.getQtyIn() ) + Integer.parseInt( wipStatus.getMarking() ) ) + "" );
                                }
                                catch(Exception e)
                                {
                                    assemblyLot.setQtyIn( wipStatus.getMarking() );
                                }
                            }
                            else
                            {
                                assemblyLot.setQtyIn( wipStatus.getMarking() );
                            }
                            if ( assemblyLot.getStage() != null && !"Marking".equalsIgnoreCase( assemblyLot.getStage() ) )
                            {
                                assemblyLot.setStage ( assemblyLot.getStage() +", Marking") ;
                            }
                            else
                            {
                                assemblyLot.setStage("Marking");
                            }

                        }
                        if( wipStatus.getMfQty() != null  )
                        {
                            assemblyLot = (AssemblyLot) m_assyWipDataMap.get(partNumber + lotNumber + fabLotNumber);

                            if( assemblyLot.getQtyIn() != null )
                            {
                                try
                                {
                                    assemblyLot.setQtyIn( ( Integer.parseInt( assemblyLot.getQtyIn() ) + Integer.parseInt( wipStatus.getMfQty() ) ) + "" );
                                }
                                catch(Exception e)
                                {
                                    assemblyLot.setQtyIn( wipStatus.getMfQty() );
                                }
                            }
                            else
                            {
                                assemblyLot.setQtyIn( wipStatus.getMfQty() );
                            }
                            if ( assemblyLot.getStage() != null && !"Metal Finish".equalsIgnoreCase( assemblyLot.getStage() ) )
                            {
                                assemblyLot.setStage ( assemblyLot.getStage() +", Metal Finish") ;
                            }
                            else
                            {
                                assemblyLot.setStage("Metal Finish");
                            }

                        }
                        if( wipStatus.getTfQty() != null )
                        {
                            assemblyLot = (AssemblyLot) m_assyWipDataMap.get(partNumber + lotNumber + fabLotNumber);

                            if( assemblyLot.getQtyIn() != null )
                            {
                                try
                                {
                                    assemblyLot.setQtyIn( ( Integer.parseInt( assemblyLot.getQtyIn() ) + Integer.parseInt( wipStatus.getTfQty() ) ) + "" );
                                }
                                catch(Exception e)
                                {
                                    assemblyLot.setQtyIn( wipStatus.getTfQty() );
                                }
                            }
                            else
                            {
                                assemblyLot.setQtyIn( wipStatus.getTfQty() );
                            }
                            if ( assemblyLot.getStage() != null && !"Trim Form".equalsIgnoreCase( assemblyLot.getStage() ) )
                            {
                                assemblyLot.setStage ( assemblyLot.getStage() +", Trim Form") ;
                            }
                            else
                            {
                                assemblyLot.setStage("Trim Form");
                            }

                        }
                        if( wipStatus.getFvQty() != null )
                        {
                            assemblyLot = (AssemblyLot) m_assyWipDataMap.get(partNumber + lotNumber + fabLotNumber);

                            if( assemblyLot.getQtyIn() != null )
                            {
                                try
                                {
                                    assemblyLot.setQtyIn( ( Integer.parseInt( assemblyLot.getQtyIn() ) + Integer.parseInt( wipStatus.getFvQty() ) ) + "" );
                                }
                                catch(Exception e)
                                {
                                    assemblyLot.setQtyIn( wipStatus.getFvQty() );
                                }
                            }
                            else
                            {
                                assemblyLot.setQtyIn( wipStatus.getFvQty() );
                            }
                            if ( assemblyLot.getStage() != null && !"Final Visual".equalsIgnoreCase( assemblyLot.getStage() ) )
                            {
                                assemblyLot.setStage ( assemblyLot.getStage() +", Final Visual") ;
                            }
                            else
                            {
                                assemblyLot.setStage("Final Visual");
                            }
                        }

                        if( wipStatus.getFinalTest() != null )
                        {
                            assemblyLot = (AssemblyLot) m_assyWipDataMap.get(partNumber + lotNumber + fabLotNumber);

                            if( assemblyLot.getQtyIn() != null )
                            {
                                try
                                {
                                    assemblyLot.setQtyIn( ( Integer.parseInt( assemblyLot.getQtyIn() ) + Integer.parseInt( wipStatus.getFinalTest() ) ) + "" );
                                }
                                catch(Exception e)
                                {
                                    assemblyLot.setQtyIn( wipStatus.getFinalTest() );
                                }
                            }
                            else
                            {
                                assemblyLot.setQtyIn( wipStatus.getFinalTest() );
                            }
                            if ( assemblyLot.getStage() != null && !"Final Test".equalsIgnoreCase( assemblyLot.getStage() ) )
                            {
                                assemblyLot.setStage ( assemblyLot.getStage() +", Final Test") ;
                            }
                            else
                            {
                                assemblyLot.setStage("Final Test");
                            }

                        }

                        if( wipStatus.getFgQty() != null )
                        {
                            assemblyLot = (AssemblyLot) m_assyWipDataMap.get(partNumber + lotNumber + fabLotNumber);

                            if( assemblyLot.getQtyIn() != null )
                            {
                                try
                                {
                                    assemblyLot.setQtyIn( ( Integer.parseInt( assemblyLot.getQtyIn() ) + Integer.parseInt( wipStatus.getFgQty() ) ) + "" );
                                }
                                catch(Exception e)
                                {
                                    assemblyLot.setQtyIn( wipStatus.getFgQty() );
                                }
                            }
                            else
                            {
                                assemblyLot.setQtyIn( wipStatus.getFgQty() );
                            }
                            if ( assemblyLot.getStage() != null && !"ASSY-FG".equalsIgnoreCase( assemblyLot.getStage() ) )
                            {
                                assemblyLot.setStage ( assemblyLot.getStage() +", ASSY-FG") ;
                            }
                            else
                            {
                                assemblyLot.setStage("ASSY-FG");
                            }

                        }

                        if ( assemblyLot.getQtyIn() != null && assemblyLot.getQtyIn().equalsIgnoreCase("0") == false )
                        {
                            assemblyLot.setLotNumber( lotNumber );
                            assemblyLot.setDeviceName( partNumber );
                            assemblyLot.setStatus( LOT_STATUS_RUNNING );
                            assemblyLot.setFabLotNumber( lotNumber );
                            assemblyLot.setPackageType( wipStatus.getPkgType() );
                            assemblyLot.setPONumber( wipStatus.getPoNumber() );
                            assemblyLot.setCategory( LOT_CATEGORY_TYPE_PROD );
                            if ( dateIn != null )
                            {
                                assemblyLot.setDateIn( dateIn );
                            }
                            else
                            {
                                assemblyLot.setDateIn( receiveDate );
                            }
                            assemblyLot.setExpectedDateOut( assyExpdateOut );
                            assemblyLot.setProcessType( WIP_TYPE_ASSEMBLY );
                        }
                    }
                }
            }
        }
        catch( Exception e)
        {
            System.out.println("ERROR!! ERROR!! While parsing the UNISEM Assy Wip Data Details........" + e );
            e.printStackTrace();
            throw e;
        }
       
    }
   /**
    *
    * @param message
    */
   private static void log( String message )
   {

       if ( message == null ) return;

       if ( m_parserLogger != null )
       {
           synchronized ( m_parserLogger )
           {
               m_parserLogger.logDebug ( message + LINE_SEPARATOR );
           }
       }
       else
       {
           System.out.println ( message );
       }
   }
}

